(function(){

'use strict';

angular.module('controllers.faultLocation.pbossOm')
  .controller('dataBaseController', dataBaseController);

function dataBaseController($scope, resourceServer, $state, COREDATA,dataBaseServer,resourceUseRateService) {
  $scope.errorInfo = COREDATA.ERRORINFO;
  $scope.timeTypes = [{
    timeTypeName: '天',
    timeTypeCode: '1'
  }, {
    timeTypeName: '小时',
    timeTypeCode: '0'
  }];
  $scope.yearitem = {};
  $scope.param = {};
  $scope.shoumun = 0;
  $scope.systems = [{
    systemName: '统一支付',
    systemCode: '11'
  }, {
    systemName: '物联网',
    systemCode: '22'
  }];
  $scope.regions = resourceUseRateService.getRegion();
  $scope.yearitem =dataBaseServer.yearitem;

  $scope.param = dataBaseServer.param;
    var year = new Date().getFullYear();
    var month = new Date().getMonth();
    var day = new Date().getDate();
    var hour = new Date().getHours();
      $scope.$watch('param.timeType', function(newValue) {
      switch (newValue) {
        case '1':
          $scope.yearitem.startTime = new Date(year, month, day-7);
          $scope.yearitem.endTime = new Date(year, month, day);
          break;
        case '0':
          $scope.yearitem.startTime = new Date(year, month, day, hour-7);
          $scope.yearitem.endTime = new Date(year, month, day, hour);
      }
    });

  function charitem(res) {
    var insertOption = Echarts(res.data.insert, '主机插入性能', '时间(秒)', 'line');
    insert.setOption(insertOption);
    var selectOption = Echarts(res.data.select, '主机查询性能', '时间(秒)', 'line');
    select.setOption(selectOption);
    var updateOption = Echarts(res.data.update, '主机更新性能', '时间(秒)', 'line');
    update.setOption(updateOption);
    var equipmentSpaceOption = Echarts(res.data.equipmentSpace, '主机设备空间使用率', '使用率%', 'bar');
    equipmentSpace.setOption(equipmentSpaceOption);
    var dataSpaceOption = Echarts(res.data.dataSpace, '主机数据空间使用率', '使用率', 'bar');
    dataSpace.setOption(dataSpaceOption);
    var ecConfig = echarts.config;
    insert.on(ecConfig.EVENT.CLICK, drill);
    select.on(ecConfig.EVENT.CLICK, drill);
    update.on(ecConfig.EVENT.CLICK, drill);
    equipmentSpace.on(ecConfig.EVENT.CLICK, drill);
    equipmentSpace.on(ecConfig.EVENT.CLICK, drill);
    dataSpace.on(ecConfig.EVENT.CLICK, drill);
  }


  //function requert(value) {
  //  var param = {};
  //  param.timeType = value;
  //  param.startTime = value === '1' ? $filter('date')($scope.yearitem.startTime, 'yyyy-MM-dd') : $filter('date')($scope.yearitem.startTime, 'yyyy-MM-dd HH');
  //  param.endTime = value === '1' ? $filter('date')($scope.yearitem.endTime, 'yyyy-MM-dd') : $filter('date')($scope.yearitem.endTime, 'yyyy-MM-dd HH');
  //  param.systemCode = $scope.param.systemCode;
  //  param.region = $scope.param.regionName;
  //  return param;
  //}

  resourceServer.dataBase(charitem);



  var insert = echarts.init(document.getElementById('insert'));
  var select = echarts.init(document.getElementById('select'));
  var update = echarts.init(document.getElementById('update'));
  var equipmentSpace = echarts.init(document.getElementById('equipmentSpace'));
  var dataSpace = echarts.init(document.getElementById('dataSpace'));

  $scope.queryAllChannel = function() {
    $scope.shoumun == 1 ? $scope.shoumun = 0 : $scope.shoumun = 1;
    if ($scope.shoumun === 0) {
      resourceServer.dataBase(charitem);
    }
  };

  function drill() {
    $state.go('sas.oldOMDisplay.sysResource.dataBaseson');
  }



  function Echarts(data, titleName, xname, charType) {
    var option = {};
    option.series = [];
    option.title = {
      text: titleName
    };
    option.tooltip = {
      trigger: 'axis'
    };
    option.legend = {
      data: [],
      y: 40
    };
    option.calculable = true;
    option.xAxis = [{
      name: '日期',
      type: 'category',
      boundaryGap: true,
      data: data[0].time
    }];
    option.yAxis = [{
      type: 'value',
      name: xname
    }];
    for (var i = 0; i < data.length; i++) {
      option.series.push(chardate(data[i], charType));
      option.legend.data.push(data[i].hostIp);
    }
    return option;
  }

  function chardate(data, charType) {
    return {
      type: charType,
      data: data.responseTime,
      itemStyle: {
        normal: {
          label: {
            show: true,
            position: 'top'
          }
        }
      },
      name: data.hostIp,
      barMaxWidth: 13
    };
  }

}


dataBaseController.$inject = ['$scope', 'resourceServer', '$state', 'COREDATA','dataBaseServer','resourceUseRateService'];

})();